package com.hanhai.material.device.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.result.KingdeeResult;
import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysBomChild;
import com.hanhai.material.device.entity.SysMaterialRecord;
import com.hanhai.material.device.model.pick.Data;
import com.hanhai.material.device.model.pick.PickData;
import com.hanhai.material.device.service.SysBomChildService;
import com.hanhai.material.device.service.SysMaterialRecordService;
import com.hanhai.material.device.service.SysProductRecordService;
import com.hanhai.material.device.utils.kingdee.KingdeeUtils;
import com.hanhai.material.utils.redis.RedisUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

import static com.hanhai.material.device.entity.table.SysBomChildTableDef.SYS_BOM_CHILD;
import static com.hanhai.material.device.entity.table.SysMaterialRecordTableDef.SYS_MATERIAL_RECORD;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.mybatisflex.core.query.QueryMethods.distinct;
import static com.mybatisflex.core.query.QueryMethods.sum;

/**
 * 控制层。
 *
 * @author youchuang
 * @since 2023/10/24 11:14:26
 */
@RestController
@RequestMapping("/material_record")
@Log4j2
public class SysMaterialRecordController {

    @Autowired
    private SysMaterialRecordService sysMaterialRecordService;

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    KingdeeUtils kingdeeUtils;
    @Autowired
    SysProductRecordService sysProductRecordService;


    /**
     * 添加。
     *
     * @param sysMaterialRecord
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public Result<?> save(@RequestBody SysMaterialRecord sysMaterialRecord) throws IOException {
        System.out.println("Material:" + sysMaterialRecord.toString());
        Date date = new Date();
        String id = fromDateToString("yyyyMMdd", sysMaterialRecord.getProductDate())
                + sysMaterialRecord.getWorkPlanId()
                + sysMaterialRecord.getMaterialId()
                + sysMaterialRecord.getMachineName()
                + fromDateToString("yyyyMMddHHmmss", date);
        sysMaterialRecord.setId(UUID.randomUUID().toString());
        Date now = new Date();
        LocalDate localDate = LocalDate.now();
        int currentHour = now.getHours();
        String shift = null;
        if (currentHour >= 8 && currentHour < 20) {
            shift = "白班";
        } else {
            shift = "夜班";
        }
        if (currentHour >= 0 && currentHour < 8) {
//            this.date = moment(this.date).add(-1)
            localDate = localDate.minusDays(1);
        }
        Date fdate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());


//        KingdeeResult kingdeeResult=new KingdeeResult();
//        kingdeeResult=this.addPickList(sysMaterialRecord);
//        if(kingdeeResult.getStatusCode()!=200){
//            return ResultTool.fail(kingdeeResult);
//        }
        sysMaterialRecord.setShift(shift);
        boolean result = this.sysMaterialRecordService.save(sysMaterialRecord);
        if (result) {
//            QueryWrapper queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_MATERIAL_RECORD.WORK_PLAN_ID.eq(sysMaterialRecord.getWorkPlanId()));
//            List<SysMaterialRecord> materialRecordList = this.sysMaterialRecordService.list(queryWrapper);
            return ResultTool.success();
        }
        return ResultTool.fail();
    }

    @PostMapping("save1")
    public Result<?> save1(@RequestBody List<SysMaterialRecord> sysMaterialRecordList) throws IOException {
        String workPlanId = null;
        for (SysMaterialRecord sysMaterialRecord : sysMaterialRecordList) {
            workPlanId = sysMaterialRecord.getWorkPlanId();

            System.out.println("Material:" + sysMaterialRecord.toString());
            Date date = new Date();
            String id = fromDateToString("yyyyMMdd", sysMaterialRecord.getProductDate())
                    + sysMaterialRecord.getWorkPlanId()
                    + sysMaterialRecord.getMaterialId()
                    + sysMaterialRecord.getMachineName()
                    + fromDateToString("yyyyMMddHHmmss", date);
            sysMaterialRecord.setId(UUID.randomUUID().toString());

            Date now = new Date();
            LocalDate localDate = LocalDate.now();
            int currentHour = now.getHours();
            String shift = null;
            if (currentHour >= 8 && currentHour < 20) {
                shift = "白班";
            } else {
                shift = "夜班";
            }
            if (currentHour >= 0 && currentHour < 8) {
//            this.date = moment(this.date).add(-1)
                localDate = localDate.minusDays(1);
            }
            Date fdate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            for(SysMaterialRecord sysMaterialRecord1:sysMaterialRecordList){
                sysMaterialRecord1.setShift(shift);
            }
//        QueryWrapper queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_RECORD.WORK_PLAN_ID.eq(sysMaterialRecord.getWorkPlanId()));
//        queryWrapper.where(SYS_PRODUCT_RECORD.ACTUAL_END_TIME.isNull(true));
//        queryWrapper.where(SYS_PRODUCT_RECORD.IS_NORMAL.eq(true));
//        queryWrapper.limit(1);
//        SysProductRecord sysProductRecord = this.sysProductRecordService.getOne(queryWrapper);
        }
        boolean result = this.sysMaterialRecordService.saveBatch(sysMaterialRecordList);
        if (result) {
//            QueryWrapper queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_MATERIAL_RECORD.WORK_PLAN_ID.eq(workPlanId));
//            List<SysMaterialRecord> materialRecordList = this.sysMaterialRecordService.list(queryWrapper);
            return ResultTool.success();
        }
        return ResultTool.fail();
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public Result<?> remove(@PathVariable Serializable id) {
        log.error("ID:{}", id);
        boolean result = this.sysMaterialRecordService.removeById(id);
        log.error("Result:{}", result);
        return result ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * 根据主键更新。
     *
     * @param sysMaterialRecord
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    public Result<?> update(@RequestBody SysMaterialRecord sysMaterialRecord) {
        boolean result = this.sysMaterialRecordService.updateById(sysMaterialRecord);
        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list();
        if (result) {
            QueryWrapper queryWrapper = QueryWrapper.create();
//                queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.eq(sysMaterialRecord.getMaterialId()));
            queryWrapper.where(SYS_MATERIAL_RECORD.WORK_PLAN_ID.eq(sysMaterialRecord.getWorkPlanId()));
//                queryWrapper.where(SYS_MATERIAL_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysMaterialRecord.getProductDate())));
            sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);
            return ResultTool.success(sysMaterialRecordList);
        } else {
            return ResultTool.fail();

        }
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @PostMapping("list/{materialId}")
    public Result<?> list(@RequestBody SysMaterialRecord sysMaterialRecord, @PathVariable String materialId) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        log.error("MaterialRecord:{}", sysMaterialRecord);
        queryWrapper.select(SYS_MATERIAL_RECORD.ID, SYS_MATERIAL_RECORD.MATERIAL_ID, SYS_MATERIAL_RECORD.BOM_ID, SYS_MATERIAL_RECORD.WORK_PLAN_ID, SYS_MATERIAL_RECORD.PRODUCT_DATE, sum(SYS_MATERIAL_RECORD.NUM).as("num"), SYS_MATERIAL_RECORD.UNIT_NAME, SYS_MATERIAL_RECORD.HAS_GET, SYS_MATERIAL_RECORD.CREATE_TIME);
        queryWrapper.where(SYS_MATERIAL_RECORD.TYPE.eq(sysMaterialRecord.getType()));
        queryWrapper.where(SYS_MATERIAL_RECORD.WORK_PLAN_ID.eq(sysMaterialRecord.getWorkPlanId()));
        if (!materialId.equals("other")) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.likeLeft(materialId));
        }else{
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("7.Y"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F11"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F26"));
        }
        log.error("SQL:",queryWrapper.toSQL());

        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(true));
        queryWrapper.groupBy(SYS_MATERIAL_RECORD.MATERIAL_ID, SYS_MATERIAL_RECORD.HAS_GET);
        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);

        return sysMaterialRecordList.isEmpty() ? ResultTool.fail() : ResultTool.success(sysMaterialRecordList);
    }

    @PostMapping("list1/{materialId}")
    public Result<?> list1(@RequestBody SysMaterialRecord sysMaterialRecord, @PathVariable String materialId) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        log.error("MaterialRecord:{}", sysMaterialRecord);
        queryWrapper.where(SYS_MATERIAL_RECORD.TYPE.eq(sysMaterialRecord.getType()));
        queryWrapper.where(SYS_MATERIAL_RECORD.WORK_PLAN_ID.eq(sysMaterialRecord.getWorkPlanId()));
        if (!materialId.equals("other")) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.likeLeft(materialId));
        }else{
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("7.Y"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F11"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F26")).or(SYS_MATERIAL_RECORD.MATERIAL_NAME.like("麦拉"));

        }
        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(true));
        queryWrapper.orderBy(SYS_MATERIAL_RECORD.CREATE_TIME,false);

        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);

        return sysMaterialRecordList.isEmpty() ? ResultTool.fail() : ResultTool.success(sysMaterialRecordList);
    }

    @Autowired
    SysBomChildService sysBomChildService;

    @PostMapping("listByMachineName")
    public Result<?> listByMachineName(@RequestBody SysMaterialRecord sysMaterialRecord) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        log.error("MaterialRecord:{}", sysMaterialRecord);
        queryWrapper.where(SYS_MATERIAL_RECORD.MACHINE_NAME.eq(sysMaterialRecord.getMachineName()));
        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(sysMaterialRecord.isHasGet()));
        queryWrapper.where(SYS_MATERIAL_RECORD.WORK_PLAN_ID.eq(sysMaterialRecord.getWorkPlanId()));
        queryWrapper.where(SYS_MATERIAL_RECORD.TYPE.eq(sysMaterialRecord.getType()));
        if(sysMaterialRecord.isHasGet()){
            queryWrapper.where(SYS_MATERIAL_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysMaterialRecord.getProductDate())));
        }
        if (!sysMaterialRecord.equals("other")) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.likeLeft(sysMaterialRecord.getMaterialId()));
        }else{
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("7.Y"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F11"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F26"));
        }
        queryWrapper.orderBy(SYS_MATERIAL_RECORD.CREATE_TIME, true);
        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);
        for (SysMaterialRecord sysMaterialRecord1 : sysMaterialRecordList) {
            if (sysMaterialRecord1.getBomId() != null) {
                QueryWrapper queryWrapper1 = QueryWrapper.create();
                queryWrapper1.where(SYS_BOM_CHILD.MATERIAL_ID.eq(sysMaterialRecord1.getMaterialId()));
                queryWrapper1.where(SYS_BOM_CHILD.BOM_ID.eq(sysMaterialRecord1.getBomId()));
                queryWrapper1.limit(1);
                queryWrapper1.orderBy(SYS_BOM_CHILD.CREATE_TIME,false);
                log.error("SQL:{}", queryWrapper1.toSQL());
                SysBomChild sysBomChild = this.sysBomChildService.getOne(queryWrapper1);
                if (sysBomChild != null) {
                    sysMaterialRecord1.setBomChild(sysBomChild);
                }
            }
        }
        log.error("SQL:{}", queryWrapper.toSQL());
        return sysMaterialRecordList.isEmpty() ? ResultTool.fail() : ResultTool.success(sysMaterialRecordList);
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public SysMaterialRecord getInfo(@PathVariable Serializable id) {
        return sysMaterialRecordService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<SysMaterialRecord> page(Page<SysMaterialRecord> page) {
        return sysMaterialRecordService.page(page);
    }

    @PostMapping("/getWorkPlanId/{hasGet}/{productDate}/{materialId}")
    public Result<?> getWorkPlanId(@PathVariable boolean hasGet, @PathVariable String productDate,@PathVariable String materialId) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(hasGet));
        queryWrapper.select(distinct(SYS_MATERIAL_RECORD.MACHINE_NAME));
        queryWrapper.where(SYS_MATERIAL_RECORD.PRODUCT_DATE.eq(productDate));
        queryWrapper.orderBy(SYS_MATERIAL_RECORD.MACHINE_NAME, true);
        if (!materialId.equals("other")) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.likeLeft(materialId));
        }else{
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("7.Y"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F11"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F26"));
        }
        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);
        log.error("QuerySQL:{}", queryWrapper.toSQL());
        queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(hasGet));
        queryWrapper.where(SYS_MATERIAL_RECORD.PRODUCT_DATE.eq(productDate));
        queryWrapper.select(distinct(SYS_MATERIAL_RECORD.PRODUCT_DATE));
        if (!materialId.equals("other")) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.likeLeft(materialId));
        }else{
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("7.Y"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F11"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F26"));
        }
        log.error("SQL:{}",queryWrapper.toSQL());

        List<SysMaterialRecord> sysMaterialRecordList1 = this.sysMaterialRecordService.list(queryWrapper);
        Map<Object, Object> map = new HashMap<>();
        map.put("workPlan", sysMaterialRecordList);
        map.put("productDate", sysMaterialRecordList1);
        return (sysMaterialRecordList.isEmpty() && sysMaterialRecordList1.isEmpty()) ? ResultTool.fail() : ResultTool.success(map);
    }

    @PostMapping("/getRecordByMachine/{productDate}/{materialId}")
    public Result<?> getRecordByMachine(@PathVariable String productDate,@PathVariable String materialId) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_MATERIAL_RECORD.PRODUCT_DATE.eq(productDate));
        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(true));
        if (!materialId.equals("other")) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.likeLeft(materialId));
        }else{
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("7.Y"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F11"));
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.notLikeLeft("8.F26"));
        }
        log.error("SQL:",queryWrapper.toSQL());
        queryWrapper.orderBy(SYS_MATERIAL_RECORD.MACHINE_NAME, true);
        queryWrapper.orderBy(SYS_MATERIAL_RECORD.CREATE_TIME, true);

        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);
        return sysMaterialRecordList.isEmpty() ? ResultTool.fail() : ResultTool.success(sysMaterialRecordList);
    }


    @PostMapping("/singleMake")
    public Result<?> singleMake(@RequestBody List<SysMaterialRecord> materialRecordList) throws IOException {
        log.error("记录：{}",materialRecordList);
        return pushToKingDee(materialRecordList);
    }

    @PostMapping("/allMake")
    public Result<?> allMake() throws IOException {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(false));
        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);
        return pushToKingDee(sysMaterialRecordList);
    }
    @PostMapping("/singleMake1")
    public Result<?> singleMake1(@RequestBody List<SysMaterialRecord> materialRecordList) throws IOException {
        log.error("记录：{}",materialRecordList);
        for(SysMaterialRecord materialRecord:materialRecordList){
            List<SysMaterialRecord> materialRecords=new ArrayList<>();
            materialRecords.add(materialRecord);
            pushToKingDee1(materialRecords);
        }
        return ResultTool.success();
    }

    @PostMapping("/allMake1")
    public Result<?> allMake1() throws IOException {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_MATERIAL_RECORD.HAS_GET.eq(false));
        List<SysMaterialRecord> sysMaterialRecordList = this.sysMaterialRecordService.list(queryWrapper);
        return pushToKingDee1(sysMaterialRecordList);
    }

    //    @PostMapping("/pushToKingDee")
    public Result<?> pushToKingDee(List<SysMaterialRecord> materialRecordList) throws IOException {
        Data data = new Data();
        List<Object> page1List = new ArrayList<>();
        List<Object> page2List = new ArrayList<>();
        Map<Object, Object> map1 = new HashMap<>();
        Map<Object, Object> dataMap = new HashMap<>();
        for (SysMaterialRecord sysMaterialRecord : materialRecordList) {
            JSONObject jsonObject = JSONObject.parseObject(sysMaterialRecord.getMaterialRecord());
            log.error("JSONOBject:{}", jsonObject);
            JSONObject objectData = jsonObject.getJSONObject("Data");
            JSONArray jsonArray1 = objectData.getJSONArray("Page1");
            Object object1 = jsonArray1.get(0);
            JSONArray jsonArray2 = objectData.getJSONArray("Page2");
            Object object2 = jsonArray2.get(0);
            page1List.add(object1);
            page2List.add(object2);

            log.error("Object:{}", object1);

        }

        map1.put("Page1", page1List);
        map1.put("Page2", page2List);
        dataMap.put("Data", map1);
        PickData pickData = new PickData();
        pickData.setData(data);
        log.error("JSON:{}", JSONObject.toJSONString(dataMap));
        String materialRecord = JSONObject.toJSONString(pickData);
        log.error("PickData:{}",JSONObject.toJSONString(pickData));
//        KingdeeResult kingdeeResult=new KingdeeResult();
        KingdeeResult kingdeeResult = this.kingdeeUtils.addPick1(JSONObject.parseObject(JSONObject.toJSONString(dataMap)));
        if (kingdeeResult.getStatusCode() == 200) {
            for (SysMaterialRecord sysMaterialRecord : materialRecordList) {
                sysMaterialRecord.setHasGet(true);
                JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                log.error("JSONOBject:{}", jsonObject);
                try {
                    sysMaterialRecord.setKingId(jsonObject.getString("BillNo"));
                    sysMaterialRecord.setResultIntId(jsonObject.getString("BillInterID"));
                } catch (Exception e) {

                }
            }
            this.sysMaterialRecordService.updateBatch(materialRecordList);
        }
        log.error("制单结果：{}", JSONObject.toJSONString(kingdeeResult));
        return kingdeeResult.getStatusCode() == 200 ? ResultTool.success(kingdeeResult) : ResultTool.fail(kingdeeResult);
    }
    public Result<?> pushToKingDee1(List<SysMaterialRecord> materialRecordList) throws IOException {
        Data data = new Data();
        List<Object> page1List = new ArrayList<>();
        List<Object> page2List = new ArrayList<>();
        Map<Object, Object> map1 = new HashMap<>();
        Map<Object, Object> dataMap = new HashMap<>();
        for (SysMaterialRecord sysMaterialRecord : materialRecordList) {
            JSONObject jsonObject = JSONObject.parseObject(sysMaterialRecord.getMaterialRecord());
            log.error("JSONOBject:{}", jsonObject);
            JSONObject objectData = jsonObject.getJSONObject("Data");
            JSONArray jsonArray1 = objectData.getJSONArray("Page1");
            Object object1 = jsonArray1.get(0);
            JSONArray jsonArray2 = objectData.getJSONArray("Page2");
            Object object2 = jsonArray2.get(0);
            page1List.add(object1);
            page2List.add(object2);

            log.error("Object:{}", object1);

        }

        map1.put("Page1", page1List);
        map1.put("Page2", page2List);
        dataMap.put("Data", map1);
        PickData pickData = new PickData();
        pickData.setData(data);
        log.error("JSON:{}", JSONObject.toJSONString(dataMap));
        String materialRecord = JSONObject.toJSONString(pickData);
        log.error("PickData:{}",JSONObject.toJSONString(pickData));
//        KingdeeResult kingdeeResult=new KingdeeResult();
        KingdeeResult kingdeeResult = this.kingdeeUtils.addPick2(JSONObject.parseObject(JSONObject.toJSONString(dataMap)));
        if (kingdeeResult.getStatusCode() == 200) {
            for (SysMaterialRecord sysMaterialRecord : materialRecordList) {
                sysMaterialRecord.setHasGet(true);
                JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                log.error("JSONOBject:{}", jsonObject);
                try {
                    sysMaterialRecord.setKingId(jsonObject.getString("BillNo"));
                    sysMaterialRecord.setResultIntId(jsonObject.getString("BillInterID"));
                } catch (Exception e) {

                }
            }
            this.sysMaterialRecordService.updateBatch(materialRecordList);
        }
        log.error("制单结果：{}", JSONObject.toJSONString(kingdeeResult));
        return kingdeeResult.getStatusCode() == 200 ? ResultTool.success(kingdeeResult) : ResultTool.fail(kingdeeResult);
    }


    @PostMapping("/admin/list/{pageNumber}/{pageSize}")
    public Result<?> adminGetPageList(@PathVariable long pageNumber, @PathVariable long pageSize,
                                      @RequestBody SysMaterialRecord sysMaterialRecord) {
        Page<SysMaterialRecord> page = new Page<>(pageNumber, pageSize);
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (sysMaterialRecord.getWorkPlanId() != null && !sysMaterialRecord.getWorkPlanId().isEmpty()) {
            queryWrapper.where(SYS_MATERIAL_RECORD.WORK_PLAN_ID.like(sysMaterialRecord.getWorkPlanId()));
        }
        if (sysMaterialRecord.getMachineName() != null && !sysMaterialRecord.getMachineName().isEmpty()) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MACHINE_NAME.eq(sysMaterialRecord.getMachineName()));
        }
        if (sysMaterialRecord.getProductDate() != null) {
            queryWrapper.where(SYS_MATERIAL_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", sysMaterialRecord.getProductDate())));
        }
        if (sysMaterialRecord.getMaterialId() != null && !sysMaterialRecord.getMaterialId().isEmpty()) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_ID.like(sysMaterialRecord.getMaterialId()));
        }
        if (sysMaterialRecord.getMaterialName() != null && !sysMaterialRecord.getMaterialName().isEmpty()) {
            queryWrapper.where(SYS_MATERIAL_RECORD.MATERIAL_NAME.like(sysMaterialRecord.getMaterialName()));
        }
        Page<SysMaterialRecord> sysMaterialRecordPage = this.sysMaterialRecordService.getMapper().paginateWithRelations(page, queryWrapper);
        log.error(queryWrapper.toSQL());
        return sysMaterialRecordPage.getRecords().isEmpty() ? ResultTool.fail() : ResultTool.success(sysMaterialRecordPage);
    }

}
